<template>
  <div class="container">
    <h1>购物平台后台管理系统</h1>
    <div class="box">
      <div class="left" />
      <div class="right">
        <h4>登 录</h4>
        <!-- 表单 -->
        <div class="form_box">
          <el-form
            ref="loginForm"
            class="form"
            :model="loginForm"
            :rules="rules"
            autocomplete="on"
            label-position="left"
          >
            <!-- <div style="text-align:center;height:60px;line-height:60px;font-weight:bold">用户名密码登录</div> -->
            <el-form-item prop="username" label="用户名:" label-width="70px" style="margin-bottom:3em">
              <el-input
                ref="username"
                v-model="loginForm.username"
                class="input"
                placeholder="请输入用户名"
                type="text"
              />
            </el-form-item>
            <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
              <el-form-item prop="password" label="密码:" label-width="70px">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  class="input"
                  :type="passwordType"
                  placeholder="请输入密码"
                  show-password
                />
              </el-form-item>
            </el-tooltip>
            <div>
              <input class="submit1 submit" type="button" value="登录" @click="handleLogin" />
              <input class="submit2 submit" type="button" value="注册" @click="register" />
            </div>
          </el-form>
          <!-- 表单 -->
        </div>
      </div>
    </div>
    <RegisterDialog :isshow.sync="isShow" />
  </div>
</template>

<script>
import { post } from '@/utils/request'
import RegisterDialog from '@/components/Register/index.vue'
export default {
  name: 'Login',
  components: { RegisterDialog },
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   callback()
    // }
    // const validatePassword = (rule, value, callback) => {
    //   callback()
    // }
    return {
      visible: false,
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loginForm: {
        username: '',
        password: ''
      },
      // loginRules: {
      //   username: [
      //     { required: true, trigger: 'blur', validator: validateUsername }
      //   ],
      //   password: [
      //     { required: true, trigger: 'blur', validator: validatePassword }
      //   ]
      // },
      passwordType: 'password',
      capsTooltip: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      isShow: false
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    // 提交注册
    submitHandler() {
      this.$refs['registerForm'].validate((valid) => {
        if (valid) {
          const url = '/user/register'
          post(url, this.form).then((response) => {
            this.$message({ type: 'success', message: response.message })
            this.visible = false
          })
        } else {
          return false
        }
      })
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['loginForm'].clearValidate()
    },
    // 去保存
    toRegisterHandler() {
      this.form = {}
      this.visible = true
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
            })
            .catch((ref) => {
              this.$message.error(ref.message)
            })
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    register() {
      this.loginForm = {}
      this.clearValidate()
      this.isShow = true
      // console.log(this.isShow)
    }
  }
}
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  font-size: 0.7rem;
  width: 100%;
  height: 100%;
  position: fixed;
  background: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%) no-repeat;
}
h1 {
  font-size: 4rem;
  text-align: center;
  margin-top: 3rem;
  text-shadow: 5px 5px 5px #ccc;
  color: #fff;
}
::selection {
  color: #fff;
  background-color: rgb(144, 129, 241);
}

.box {
  display: flex;
  overflow: hidden;
  width: 60rem;
  height: 32rem;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 1.5rem;
  margin: 4rem auto;
  box-shadow: 0 0 1rem 0.2rem rgba(0, 0, 0, 0.1);
}

.box .left {
  position: relative;
  width: 40%;
  height: 100%;
  background-color: skyblue;
}

.box .left::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url(./images/left1.png);
  background-size: 100% 100%;
  opacity: 0.8;
}

.box .right {
  // display: flex;
  width: 65%;
  // flex-direction: column;
  // align-items: center;
}

.box .right h4 {
  text-align: center;
  color: rgb(144, 129, 241);
  font-size: 2.8rem;
  margin-top: 4.5rem;
}

.box .right .form_box .form {
  // display: flex;
  // flex-direction: column;
  // align-items: stretch;
  width: 60%;
  margin-left: 10em;
  padding-top: 4em;
}
.box .right .form_box .form .password {
  padding-top: 2em;
}

.right .submit1 {
  display: inline-block;
  position: relative;
  width: 7rem;
  height: 3rem;
  color: #f6f6f6;
  background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
  font-size: 1.2rem;
  border: none;
  border-radius: 0.5rem;
  margin: 4rem 0 0 4rem;
  transform: translateX(-40%);
}
.right .submit2 {
  display: inline-block;
  position: relative;
  width: 7rem;
  height: 3rem;
  color: #f6f6f6;
  background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
  font-size: 1.2rem;
  border: none;
  border-radius: 0.5rem;
  margin: 4rem 0 0 4rem;
  transform: translateX(-10%);
}

.right .submit:hover {
  box-shadow: 0 0 2rem -0.5rem rgba(0, 0, 0, 0.15);
  cursor: pointer;
}
</style>
